//
//  Common.m
//  MyShoes
//
//  Created by mac on 11-6-20.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Common.h"
#import "iToast.h"
//#import "JSON.h"
#import "GlobalVar.h"

@implementation Common
/*
+(id)parseJsonData:(NSData *)jsonData
{
    NSString *dataString = [[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]autorelease];
    //NSLog(@"%@",dataString);
	SBJSON *jsonParser = [[[SBJSON alloc]init]autorelease];
	NSError *parseError = nil;
	id result = [jsonParser objectWithString:dataString error:&parseError];
    return result;
}*/

+(ASIHTTPRequest *)createHTTPRequest:(NSString *)url delegate:(id<ASIHTTPRequestDelegate>)delegate
{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[GlobalVar createApiUrlAddress:url]]];
    request.delegate = delegate;
    request.username = [GlobalVar getDataForKey:@"config.username"];
    request.password = [GlobalVar getDataForKey:@"config.password"];
    [request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic];
    return request;
}

/*
+(id)parseJsonDataWithUrl:(NSString *)url
{
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    return [self parseJsonData:jsonData];
}
*/

+(NSString *)getImagePath:(NSString *)xpath
{
    NSString *result = [Common getDataPath:[NSString stringWithFormat:@"%@.png",xpath]];
    if (!result) {
        result = [Common getDataPath:[NSString stringWithFormat:@"%@.jpg",xpath]];
    }
    if (!result) {
        result = [Common getDataPath:[NSString stringWithFormat:@"%@.jpeg",xpath]];
    }
    return result;
}

+(NSString *)getDataPath:(NSString *)xpath
{
    return [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"data/%@",xpath] ofType:nil];
}

+(NSArray *)loadNode:(NSString *)xpath isDir:(BOOL)isDir
{
    NSMutableArray *result = [NSMutableArray array];
    NSString *path = [Common getDataPath:xpath];
    NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    for (NSString *aPath in contentOfFolder) {
        NSString * fullPath = [path stringByAppendingPathComponent:aPath];
        BOOL xisDir;
        if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&xisDir] && (xisDir == isDir))
        {
            //NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:aPath,@"name", nil];
            [result addObject:aPath];
        }
    }
    return result;
}

+(NSArray *)loadFileList:(NSString *)xpath
{
    return [Common loadNode:xpath isDir:NO];
}


+(NSArray *)loadDirList:(NSString *)xpath
{
    return [Common loadNode:xpath isDir:YES];
}



+(NSString *)getDateString:(NSString *)beginDate addDay:(int)addDay
{
    if (beginDate && ![beginDate isEqualToString:@""]) {
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *date = [dateFormatter dateFromString:beginDate];
        NSTimeInterval  interval = 24*60*60*(addDay-1);
        NSDate *date1 = [date dateByAddingTimeInterval:+interval];
        return [NSString stringWithFormat:@"第%d天 %@",addDay, [dateFormatter stringFromDate:date1]];
    }else{
        return [NSString stringWithFormat:@"第%d天",addDay];
    }
}

+(NSString *)formartDayString:(CGFloat)day
{
    if (day == (int)day) {
        return [NSString stringWithFormat:@"%d 天",(int)day];
    }else{
        return [NSString stringWithFormat:@"%0.1f 天",day];
    }
}

+(void)iToast:(NSString *)msg
{
    iToast *toast = [iToast makeText:msg];
    [toast show];
}

+ (NSString *)intervalSinceNow: (NSDate *) d 
{
    if (!d) {
        return @"从未更新";
    }
    /*
	 NSDateFormatter *date=[[NSDateFormatter alloc] init];
	 [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	 NSDate *d=[date dateFromString:theDate];
	 */
	
    NSTimeInterval late=[d timeIntervalSince1970]*1;
    
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    NSString *timeString=@"";
    
    NSTimeInterval cha=now-late;
    
    if (cha/3600<1) {
		
		if (cha/60<1) {
			timeString = @"刚刚";
		}else {
			timeString = [NSString stringWithFormat:@"%f", cha/60];
			timeString = [timeString substringToIndex:timeString.length-7];
			timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
		}
        
    }
    if (cha/3600>1&&cha/86400<1) {
        timeString = [NSString stringWithFormat:@"%f", cha/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@小时前", timeString];
    }
    if (cha/86400>1)
    {
        timeString = [NSString stringWithFormat:@"%f", cha/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@天前", timeString];
        
    }
    //[date release];
    return timeString;
}

+(CGFloat)dateBetweenNow:(NSString *)is_date
{
	if (!is_date) {
		return 10000 ;
	}
	
	NSDateFormatter *date=[[NSDateFormatter alloc] init];
	[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	NSDate *d=[date dateFromString:is_date];
   
	NSTimeInterval late=[d timeIntervalSince1970]*1;
	
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    
	NSTimeInterval cha=now-late;
 	
	return cha/86400;
}

+(UIImage *)getImage:(NSString *)imageName
{
    NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
    NSLog(@"%@",path);
    return [UIImage imageWithContentsOfFile:path];
}


@end
